package com.xq.jltx.frescosimple.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.xq.jltx.frescosimple.R;
import com.xq.jltx.frescosimple.utils.XqLog;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016年02月01日.
 */

public class HomeFragment extends BackHandledFragment {
    private final String TAG = HomeFragment.class.getSimpleName();
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private Button btnImgList;
    private Button btnImgGrid;
    private Button btnImgPager;
    private Button btnImgGallery;
    private Button btnImgListGrid;

    private TextView textView;
    private String param1 = "non-param";
    private int mCurrentAdapterIndex;
    private String[] imgList;

    public HomeFragment() {
    }

    /**
     * 通过这个静态工厂方法去传递参数
     * @param param1
     * @param param2
     * @return
     */
    public static HomeFragment newInstance(String param1, String[] param2,int currentIndex){
        HomeFragment homeFragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putCharSequenceArray(ARG_PARAM2,param2);
        args.putInt(ARG_PARAM3,currentIndex);
        homeFragment.setArguments(args);
        return homeFragment;
    }
    private void assignViews(View view) {
        btnImgList = (Button) view.findViewById(R.id.btn_img_list);
        btnImgGrid = (Button) view.findViewById(R.id.btn_img_grid);
        btnImgPager = (Button) view.findViewById(R.id.btn_img_pager);
        btnImgGallery = (Button) view.findViewById(R.id.btn_img_gallery);
        btnImgListGrid = (Button) view.findViewById(R.id.btn_img_list_grid);
        textView = (TextView)view.findViewById(R.id.text_view);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        XqLog.d("HomeFragment.onCreate");
        if(getArguments() != null){
            param1 = getArguments().getString(ARG_PARAM1);
            imgList = (String[])getArguments().getCharSequenceArray(ARG_PARAM2);
            mCurrentAdapterIndex = getArguments().getInt(ARG_PARAM3);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        XqLog.d("HomeFragment.onAttach");
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        XqLog.d("HomeFragment.onCreateView");
        View view =  inflater.inflate(R.layout.home_fragment,null,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        XqLog.d("HomeFragment.onViewCreated");
        assignViews(view);
        btnImgList.setOnClickListener(mOnClickListener);
        btnImgGrid.setOnClickListener(mOnClickListener);
        btnImgPager.setOnClickListener(mOnClickListener);
        btnImgGallery.setOnClickListener(mOnClickListener);
        btnImgListGrid.setOnClickListener(mOnClickListener);
        textView.setText(param1);
        super.onViewCreated(view, savedInstanceState);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            switch (view.getId()){
                case R.id.btn_img_list:
                    ImageListViewFragment imageListViewFragment = ImageListViewFragment.newInstance(imgList,mCurrentAdapterIndex);
                    //以替换的方式来切换到下一个页面
                    fragmentTransaction.replace(R.id.container, imageListViewFragment, ImageListViewFragment.class.getSimpleName());
                    fragmentTransaction.addToBackStack(ImageListViewFragment.class.getSimpleName());
                    fragmentTransaction.commit();
                    break;
                case R.id.btn_img_grid:
                    ImageGridViewFragment imageGridViewFragment = ImageGridViewFragment.newInstance(imgList,mCurrentAdapterIndex);
                    //以替换的方式来切换到下一个页面
                    fragmentTransaction.replace(R.id.container, imageGridViewFragment, ImageGridViewFragment.class.getSimpleName());
                    fragmentTransaction.addToBackStack(ImageListViewFragment.class.getSimpleName());
                    fragmentTransaction.commit();
                    break;
                case R.id.btn_img_pager:
                    break;
                case R.id.btn_img_gallery:
                    break;
                case R.id.btn_img_list_grid:
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public boolean onBackPressed() {
        XqLog.d("HomeFragment.onBackPressed");
        return false;
    }

    @Override
    public void onDestroy() {
        XqLog.d("HomeFragment.onDestroy");
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        XqLog.d("HomeFragment.onDestroyView");
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        XqLog.d("HomeFragment.onDetach");
        super.onDetach();
    }

    @Override
    public void onPause() {
        XqLog.d("HomeFragment.onPause");
        super.onPause();
    }

    @Override
    public void onResume() {
        XqLog.d("HomeFragment.onResume");
        super.onResume();
    }

    @Override
    public void onStart() {
        XqLog.d("HomeFragment.onStart");
        super.onStart();
    }

    @Override
    public void onStop() {
        XqLog.d("HomeFragment.onStop");
        super.onStop();
    }
}
